#!/bin/bash
#
# This script creates the hello_world application in this directory.


BSP_DIR=../tpk_key_bsp
QUARTUS_PROJECT_DIR=../../
NIOS2_APP_GEN_ARGS="--elf-name tpk_key.elf --set OBJDUMP_INCLUDE_SOURCE 1 --src-files hello_world.c"

# If script is launched from Windows through Windows Subsystem for Linux (WSL).  
# The adjust-path macro converts absolute windows 
# paths into unix style paths (Example: c:/dir -> /c/dir). This will ensure
# paths are readable.
uname=$(uname -r)
if [[ $uname =~ "Microsoft" ]]; then
    _IS_WSL=1
    windows_exe=.exe
fi

adjust_path() {
    if [ "${_IS_WSL}" = "1" ] && [[ ! $1 =~ ^\/mnt\/* ]]; then
        echo "$(wslpath -u "$1")"
	else
		echo "$1"
	fi
}

# First, check to see if $SOPC_KIT_NIOS2 environmental variable is set.
# This variable is required for the command line tools to execute correctly.
if [ -z "${SOPC_KIT_NIOS2}" ]
then
    echo Required \$SOPC_KIT_NIOS2 Environmental Variable is not set!
    exit 1
fi


# Also make sure that the APP has not been created already.  Check for
# existence of Makefile in the app directory
if [ -f ./Makefile ]
then
    echo Application has already been created!  Delete Makefile if you want to create a new application makefile
    exit 1
fi


# We are selecting hal_default bsp because it supports this application.
# Check to see if the hal_default has already been generated by checking for
# existence of the public.mk file.  If not, we need to run
# create-this-bsp file to generate the bsp.

PUBLIC_MK_LOC=$(adjust_path ${BSP_DIR}/public.mk)
if [ ! -f ${PUBLIC_MK_LOC} ]; then
    # Since BSP doesn't exist, create the BSP
    # Pass any command line arguments passed to this script to the BSP.
    pushd "$(adjust_path ${BSP_DIR})" >> /dev/null
    ./create-this-bsp "$@" || {
        echo "create-this-bsp failed"
        exit 1
    }
    popd >> /dev/null
fi


# Don't run make if create-this-app script is called with --no-make arg
SKIP_MAKE=
while [ $# -gt 0 ]
do
  case "$1" in
      --no-make)
          SKIP_MAKE=1
          ;;
  esac
  shift
done


# Now we also need to go copy the sources for this application to the
# local directory.
find "${SOPC_KIT_NIOS2}/examples/software/hello_world/" -name '*.c' -or -name '*.h' -or -name 'hostfs*' | xargs -i cp -L {} ./ || {
        echo "failed during copying example source files"
        exit 1
}

find "${SOPC_KIT_NIOS2}/examples/software/hello_world/" -name 'readme.txt' -or -name 'Readme.txt' | xargs -i cp -L {} ./ || {
        echo "failed copying readme file"
}

if [ -d "${SOPC_KIT_NIOS2}/examples/software/hello_world/system" ]
then
        cp -RL "${SOPC_KIT_NIOS2}/examples/software/hello_world/system" . || {
                echo "failed during copying project support files"
                exit 1
        }
fi

chmod -R +w . || {
        echo "failed during changing file permissions"
        exit 1
}

cmd="nios2-app-generate-makefile$windows_exe --bsp-dir ${BSP_DIR} --set QUARTUS_PROJECT_DIR=${QUARTUS_PROJECT_DIR} ${NIOS2_APP_GEN_ARGS}"

echo "create-this-app: Running \"${cmd}\""
$cmd || {
    echo "nios2-app-generate-makefile failed"
    exit 1
}

if [ -z "$SKIP_MAKE" ]; then
        cmd="make"

        echo "create-this-app: Running \"$cmd\""
        $cmd || {
        echo "make failed"
            exit 1
        }

        echo
        echo "To download and run the application:"
        echo "    1. Make sure the board is connected to the system."
        echo "    2. Run 'nios2-configure-sof <SOF_FILE_PATH>' to configure the FPGA with the hardware design."
        echo "    3. If you have a stdio device, run 'nios2-terminal' in a different shell."
        echo "    4. Run 'make download-elf' from the application directory."
        echo
        echo "To debug the application:"
        echo "    Import the project into Nios II Software Build Tools for Eclipse."
        echo "    Refer to Nios II Software Build Tools for Eclipse Documentation for more information."
        echo
        echo -e ""
fi


exit 0
